﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace ATM
{
    public partial class Manager : Form
    {
        private BankDatabase bd;
        public ATM atm;

        public Manager(ATM atm)
        {
            InitializeComponent();
            this.atm = atm;
            this.bd = atm.bankDatabase;
            init();
        }

        public void init()
        {
            this.container.Controls.Clear();
            this.container.Controls.Add(this.loginPage);
            if (atm.moneyAmount < 1000)
            {
                lackMoneyTip.Text = "当前ATM机金额已经不足1000元，请赶紧为ATM机充值";
            }
            currentAmount.Text = atm.moneyAmount.ToString();
            delTip.Text = "";
            addTip.Text = "";
            manageATMTip.Text = "";
            recordText.Text = atm.log.printLog();
        }

        private void loginBut_Click(object sender, EventArgs e)
        {
            if (this.PsdText.Text == "123456")
            {
                this.container.Controls.Add(this.choosePage);
                this.container.Controls.Remove(this.loginPage);
            }
            else
            {
                MessageBox.Show("您输入的管理员密码有误，请确认你是否有管理权限！","错误提示");
            }
        }

        private void manageATMBut_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.manageATMPage);
            this.container.Controls.Remove(this.choosePage);
        }

        private void recordBut_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.recordPage);
            this.container.Controls.Remove(this.choosePage);
        }

        private void manageAccountBut_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.manageAccountPage);
            this.container.Controls.Remove(this.choosePage);
        }

        private void loginOutBut_Click(object sender, EventArgs e)
        {
            init();
            this.PsdText.Text = "";
            
        }

        private void returnFromATM_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.choosePage);
            this.container.Controls.Remove(this.manageATMPage);
        }

        private void returnFromRecord_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.choosePage);
            this.container.Controls.Remove(this.recordPage);
        }

        private void returnFromAccount_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.choosePage);
            this.container.Controls.Remove(this.manageAccountPage);
        }

        private void returnFromDeleteAccount_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.manageAccountPage);
            this.container.Controls.Remove(this.deleteAccountPage);
        }

        private void returnFromAddAccount_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.manageAccountPage);
            this.container.Controls.Remove(this.addAccountPage);
        }

        private void returnFromModifyAccount_Click(object sender, EventArgs e)
        {
            this.container.Controls.Add(this.manageAccountPage);
            this.container.Controls.Remove(this.modifyAccountPage);
        }

        private void modifyAccountBut_Click(object sender, EventArgs e)
        {
            this.container.Controls.Remove(this.manageAccountPage);
            this.container.Controls.Add(this.modifyAccountPage);
        }

        private void addAccountBut_Click(object sender, EventArgs e)
        {
            this.container.Controls.Remove(this.manageAccountPage);
            this.container.Controls.Add(this.addAccountPage);
        }

        private void deleteAccountBut_Click(object sender, EventArgs e)
        {
            this.container.Controls.Remove(this.manageAccountPage);
            this.container.Controls.Add(this.deleteAccountPage);
        }

        private void cancelAddBut_Click(object sender, EventArgs e)
        {
            this.accountText.Text = "";
            this.accountPsdText.Text = "";
            this.psdAgainText.Text = "";
            this.bankText.Text = "";
            this.balanceText.Text = "";
        }

        private void addAccBut_Click(object sender, EventArgs e)
        {
            this.addTip.Text = "";
            if (accountText.Text==""||!isNumber(accountText.Text))//此处还要进行数据库中是否存在的判断
            {
                addTip.Text = "你输入的账号为空或不合法，账号只能包含数字;";
                accountText.Text = "";
                return;
            }
            if (accountPsdText.Text != psdAgainText.Text)
            {
                addTip.Text = "您两次输入的密码不符，请确认您两次输入相同的密码";
                accountPsdText.Text = "";
                psdAgainText.Text = "";
                return;
            }
            if(accountPsdText.Text == ""||!isNumber(accountPsdText.Text))
            {
                addTip.Text = "密码不能为空且必须为数字";
                accountPsdText.Text = "";
                psdAgainText.Text = "";
                return;
            }
            if (bankText.Text == "")
            {
                addTip.Text = "请选择账户所属的银行";
                return;
            }
            if (balanceText.Text == "" || !isNumber(balanceText.Text))
            {
                addTip.Text = "你输入的账户金额为空或不合法，金额只能包含数字;";
                balanceText.Text = "";
                return;
            }
            if(bd.isValidAccount(accountText.Text))
            {
                addTip.Text = "这个账户已经存在，请重新输入账户名";
                return;
            }
            Account newAccount = new Account(accountText.Text, Int32.Parse(accountPsdText.Text), Int32.Parse(balanceText.Text), bankText.Text);
            bd.addAccount(newAccount);
            bd.InsertAccount(newAccount);
            addTip.Text = "增加账户操作成功";
            return;
        }

        private Boolean isNumber(String str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (str.Substring(i, 1).ToCharArray().ElementAt(0) < '0' || str.Substring(i, 1).ToCharArray().ElementAt(0) > '9')
                {
                    return false;
                }
            }
            return true;
        }

        private void deleteAccBut_Click(object sender, EventArgs e)
        {
            this.delTip.Text = "";
            if (delAccText.Text == "" || !isNumber(delAccText.Text))//此处还要进行数据库中是否存在的判断
            {
                delTip.Text = "你输入的账号为空或不合法，账号只能包含数字;";
                delAccText.Text = "";
                return;
            }
            if (!bd.isValidAccount(delAccText.Text))
            {
                delTip.Text = "这个账户不存在，请重新输入账户名";
                return;
            }
            Account newAccount = new Account(delAccText.Text,0);
            if (bd.delAccount(delAccText.Text) == true)
            {
                delTip.Text = "删除账户操作成功";
                bd.DeleteAccount(delAccText.Text);
            }
            else
            {
                delTip.Text = "删除账户操作失败";
            }
            return;
        }

        private void cancelDelBut_Click(object sender, EventArgs e)
        {
            delAccText.Text = "";
        }

        private void cancelAddMoneyBut_Click(object sender, EventArgs e)
        {
            addMoneyAmount.Value = 10000;
        }

        private void addMoneyBut_Click(object sender, EventArgs e)
        {
            atm.moneyAmount += addMoneyAmount.Value;
            currentAmount.Text = atm.moneyAmount.ToString();
            manageATMTip.Text = "添加操作成功";
        }

        private void bankB_CheckedChanged(object sender, EventArgs e)
        {
            if (bankB.Checked == true)
            {
                bankA.Checked = false;
            }
            if (bankB.Checked == false)
            {
                bankA.Checked = true;
            }
        }

        private void bankA_CheckedChanged(object sender, EventArgs e)
        {
            if (bankA.Checked == true)
            {
                bankB.Checked = false;
            }
            if (bankA.Checked == false)
            {
                bankB.Checked = true;
            }
        }

        private void changeBank_Click(object sender, EventArgs e)
        {
            if (bankA.Checked == true)
            {
                atm.belongTo = "A";
            }
            else if (bankA.Checked == true)
            {
                atm.belongTo = "B";
            }
            manageATMTip.Text = "修改ATM所属银行成功";
        }

        private void moSureBut_Click(object sender, EventArgs e)
        {
            this.moTip.Text = "";
            if (moAccText.Text == "" || !isNumber(moAccText.Text))//此处还要进行数据库中是否存在的判断
            {
                moTip.Text = "你输入的账号为空或不合法，账号只能包含数字;";
                moAccText.Text = "";
                return;
            }
            if (moPsdText.Text != moPsdAgainText.Text)
            {
                moTip.Text = "您两次输入的密码不符，请确认您两次输入相同的密码";
                moPsdText.Text = "";
                moPsdAgainText.Text = "";
                return;
            }
            if (moPsdText.Text == "" || !isNumber(moPsdText.Text))
            {
                moTip.Text = "密码不能为空且必须为数字";
                moPsdText.Text = "";
                moPsdAgainText.Text = "";
                return;
            }
            if (moBankText.Text == "")
            {
                moTip.Text = "请选择账户所属的银行";
                return;
            }
            if (moBalanceText.Text == "" || !isNumber(moBalanceText.Text))
            {
                moTip.Text = "你输入的账户金额为空或不合法，金额只能包含数字;";
                moBalanceText.Text = "";
                return;
            }
            if (!bd.isValidAccount(moAccText.Text))
            {
                moTip.Text = "这个账户不存在，请重新输入账户名";
                return;
            }
            Account newAccount = new Account(moAccText.Text, Int32.Parse(moPsdText.Text), Int32.Parse(moBalanceText.Text), moBankText.Text);
            bd.delAccount(moAccText.Text);
            bd.addAccount(newAccount);
            moTip.Text = "修改账户操作成功";
            return;
        }

        private void moCancelBut_Click(object sender, EventArgs e)
        {
            this.moAccText.Text = "";
            this.moPsdText.Text = "";
            this.moPsdAgainText.Text = "";
            this.moBankText.Text = "";
            this.moBalanceText.Text = "";
        }

        private void outManager_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

    }
}
